<?php

// PDO数据库驱动 

class dbPdo{

    public $pdo = null;
    public $lastID = 0;
    public $affRows = 0;
    public $dbName = '';
    public $config = array();
    public $dbType = ''; //默认mysql

    // 初始化,是否支持PDO
    function __construct(){
        if(!class_exists('PDO')){
            abort('Class `PDO` NOT Found.', 602); 
        } 
    }
    // 连接数据库
    function connect($config='') { 
        $this->dbType = empty($config['db_type']) ? 'mysql' : $config['db_type'];
        if(empty($config['db_dsn'])){
            $config['db_dsn'] = $this->dbType.':host='.$config['db_host'].';dbname='.$config['db_name'].';port='.$config['db_port'].'';
        } 
        // $this->dbName = $config['db_name'];
        $this->config = $config; 
        if(!empty($config['db_conn'])) {
            $config['params'][PDO::ATTR_PERSISTENT] = true;
        }
        $this->pdo = new PDO( $config['db_dsn'], $config['db_user'], $config['db_pass']);
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        if($this->dbType=='mysql'){
            $this->pdo->exec('SET NAMES '.$config['db_cset']);
        }
    }

    // select, 0-arr, 1-row, 2-val
    function get($sql, $mode=0){
        $res = $this->pdo->query($sql);
        if($mode){ 
            $res->setFetchMode($mode==2 ? PDO::FETCH_NUM : PDO::FETCH_ASSOC); 
            return $mode==2 ? $res->fetchColumn() : $res->fetch();
        }else{
            $data = array();
            $res->setFetchMode(PDO::FETCH_ASSOC);
            $data = $res->fetchAll();
            return $data;
        }
    }
    function xxx_query($sql){
        $res = $this->pdo->query($sql); //dump($sql, $res);
        return $res;
    }
    // 执行语句(update, delete insert, other)
    function run($sql){ 
        $affRows = $this->pdo->exec($sql);
        $this->affRows = $affRows; $this->lastID = '';
        if(preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $sql)) {
             switch($this->dbType) {
                case 'PGSQL':
                case 'sqlite':
                case 'MSSQL':
                case 'SQLSRV':
                case 'IBASE':
                case 'mysql':
                    $this->lastID = $this->pdo->lastInsertId();
                    break;
                case 'ORACLE':
                case 'OCI':
                    $sequenceName = $this->table;
                    $vo = $this->pdo->query("SELECT {$sequenceName}.currval currval FROM dual");
                    $this->lastID = $vo?$vo[0]["currval"]:0;
            }
        }
        return ['lastId'=>$this->lastID, 'affRows'=>$this->affRows];
    }

    //返回quoteSql语句  //  'pea\'ce'  ->  pea\'ce
    function quoteSql($sql){
        $sql = $this->pdo->quote($sql);
        return substr($sql, 1, strlen($sql)-2);
    }

    // 释放查询结果
    function free() {
        $this->PDOStatement = null;
    }
    // 关闭数据库
    function close() {
        $this->pdo = null;
    }
    function __destruct(){
        $this->close();
    }

}
